<template>
  <div class="layout-padding">
    <splitpanes>
      <pane size="50">
        <div class="layout-padding-auto layout-padding-view">
          <el-row v-show="showSearch">
            <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
              <el-form-item :label="$t('label.name')" prop="text">
                <el-input :placeholder="$t('basDicSet.EnterDictionaryItemName')" v-model="state.queryForm.text"
                          style="max-width: 180px"/>
              </el-form-item>
              <el-form-item :label="$t('label.code')" prop="code">
                <el-input :placeholder="$t('basDicSet.EnterDictionaryItemValue')" v-model="state.queryForm.code"
                          style="max-width: 180px"/>
              </el-form-item>
              <el-form-item>
                <el-button icon="search" type="primary" @click="getDataList">
                  {{ $t('btn.search') }}
                </el-button>
                <el-button icon="Refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
                <el-button icon="plus" type="primary" class="ml10" @click="formDialogRef.openDialog()"
                           v-auth="'platform_basDicSet_add'">
                  {{$t('operation.add')}}
                </el-button>
                <el-button  :disabled="multiple" icon="Delete" type="danger"
                           v-auth="'platform_basDicSet_del'" @click="handleDelete(selectObjs)">
                  {{$t('btn.delete')}}
                </el-button>
              </el-form-item>
            </el-form>
          </el-row>
          <el-table :data="state.dataList" v-loading="state.loading" border
                    :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
                    @row-click="selectRow"
                    size="small"
                    @selection-change="selectionChangeHandle" @sort-change="sortChangeHandle">
            <el-table-column type="selection" width="40" align="center"/>
            <el-table-column type="index" label="#" width="60"/>
            <el-table-column prop="text" :label="$t('basDicSet.dictName')" show-overflow-tooltip/>
            <el-table-column prop="code" :label="$t('basDicSet.dictValue')" show-overflow-tooltip/>
            <!--<el-table-column prop="sort" label=" sort"  show-overflow-tooltip/>-->
            <!--<el-table-column prop="remark" label=" remark" show-overflow-tooltip/>-->
            <el-table-column :label="$t('operation.Operation')" width="220">
              <template #default="scope">

                <el-button icon="edit-pen"  type="primary" v-auth="'platform_basDicSet_edit'"
                           @click="formDialogRef.openDialog(scope.row.id)">
                </el-button>
                <el-button icon="delete"  type="danger" v-auth="'platform_basDicSet_del'"
                           @click="handleDelete([scope.row.id])">
                </el-button>
								<el-button  type="primary" icon="view" v-auth="'sys_role_edit'" plain
													 @click="formDialogRef.openDialog(scope.row.id, true)">
								</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
        </div>
      </pane>
      <pane size="70">
        <div class="layout-padding-auto layout-padding-view">
          <item-table ref="childRef"></item-table>
        </div>
      </pane>
    </splitpanes>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="refeshDg(false)"/>
  </div>
</template>

<script setup lang="ts" name="systemBasDicSet">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {delObjs, fetchList} from "/@/api/platform/digital/basDicSet";
import {useMessage, useMessageBox} from "/@/hooks/message";
import ItemTable from "/@/views/digital/basDicSet/itemTable.vue";
// import { useDict } from '/@/hooks/dict';
// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));

// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
const childRef = ref()

// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const parentId = ref()

// 选择行的时候触发
const selectRow = (row: any) => {
  parentId.value = row.id
  childRef.value.refresh(row.id)
}

const refeshDg = () => {
  getDataList(false)
  // 判断parentId是否为空
  if (parentId.value) {
    childRef.value.refresh(parentId.value)
  }

}


const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})


//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)


// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList(false)

}

// 导出excel
const exportExcel = () => {
  downBlobFile('/platform/basDicSet/export', Object.assign(state.queryForm, {ids: selectObjs}), 'basDicSet.xlsx')
}

// 多选事件
const selectionChangeHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>